package com.bdc.memory.service.impl;

import com.bdc.BdcVar;
import com.bdc.memory.dao.IMemoryDao;
import com.bdc.memory.dao.IRecordDao;
import com.bdc.memory.dao.ITagDao;
import com.bdc.memory.domain.MemoryInfo;
import com.bdc.memory.domain.RecordInfo;
import com.bdc.memory.domain.TagInfo;
import com.bdc.memory.service.IMemoryService;
import com.bdc.memory.service.IRecordService;
import com.bdc.utils.BdcUtils;
import com.bdc.utils.TokenUtils;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Shawn
 * Date: 2011-6-20
 * Time: 17:37:43
 */
public class IRecordServiceImpl implements IRecordService {

    @Inject
    private IRecordDao recordDao;
    @Inject
    private ITagDao tagDao;
    @Inject
    private IMemoryDao memoryDao;
    @Inject
    private IMemoryService memoryService;

    @Transactional
    public long addRecord(long userId, String memory, String description, String tag) throws Exception {
        tag = BdcUtils.filterTagString(tag);

        RecordInfo info = new RecordInfo(userId, memory, description, tag);
        long recordId = recordDao.addRecord(info);

        String[] tagArray = TokenUtils.getStringArray(tag, BdcVar.TAG_SEPERATOR);
        for (String t : tagArray) {
            tagDao.addTagAmount(userId, t);
        }

        memoryService.prepareNextMemory(recordId);

        return recordId;
    }

    @Transactional
    public void updateRecord(long userId, long recordId, String memory, String description, String tag) {
        tag = BdcUtils.filterTagString(tag);

        RecordInfo recordInfo = recordDao.getRecord(recordId);

        String[] oldTagArray = TokenUtils.getStringArray(recordInfo.getTag(), BdcVar.TAG_SEPERATOR);
        for (String t : oldTagArray) {
            tagDao.subTagAmount(userId, t);
        }

        String[] newTagArray = TokenUtils.getStringArray(tag, BdcVar.TAG_SEPERATOR);
        for (String t : newTagArray) {
            tagDao.addTagAmount(userId, t);
        }

        recordInfo.setMemory(memory);
        recordInfo.setDescription(description);
        recordInfo.setTag(tag);
    }

    @Transactional
    public void delRecord(long recordId) {
        RecordInfo recordInfo = recordDao.getRecord(recordId);

        long userId = recordInfo.getUserId();
        String[] tagArray = TokenUtils.getStringArray(recordInfo.getTag(), BdcVar.TAG_SEPERATOR);
        for (String tag : tagArray) {
            tagDao.subTagAmount(userId, tag);
        }

        memoryDao.delMemory(recordId);
        recordDao.delRecord(recordId);
    }

    public List<RecordInfo> getTagRecordList(long tagId, long from, long count) {
        TagInfo tagInfo = tagDao.getTag(tagId);

        Map<String, TagInfo> tagMap = new HashMap<String, TagInfo>();
        List<TagInfo> tagList = tagDao.getTagList(tagInfo.getUserId());
        for (TagInfo tag : tagList) {
            tagMap.put(tag.getTag(), tag);
        }

        List<RecordInfo> recordList = recordDao.getRecordList(tagInfo.getUserId(), tagInfo.getTag(), from, count);
        for (RecordInfo record : recordList) {
            String tagIdString = "";

            String tagString = record.getTag();
            String[] tagArray = TokenUtils.getStringArray(tagString, ",");
            for (String tag : tagArray) {
                TagInfo info = tagMap.get(tag);
                boolean isExist = info != null;
                if (!isExist) continue;
                tagIdString += (tagIdString.length() == 0 ? "" : ",") + info.getTagId();
            }

            record.setTagId(tagIdString);
        }

        return recordList;
    }

    public List<RecordInfo> getTodayRecordList(long userId) throws Exception {
        List<RecordInfo> recordList = new ArrayList<RecordInfo>();
        List<MemoryInfo> memoryList = memoryDao.getMemoryList(userId, System.currentTimeMillis());
        for (MemoryInfo memoryInfo : memoryList) {
            RecordInfo recordInfo = recordDao.getRecord(memoryInfo.getRecordId());
            recordList.add(recordInfo);
        }

        return recordList;
    }
}
